package router

import (
	"database/sql"
	"errors"
	"github.com/gin-gonic/gin"
	"reflect"
)

//负责和数据库有关的接口
var (
	db, _      = sql.Open("sqlite3", "myblog")
	tableNames = [...]string{"message", "messageComment", "movieNote", "movieNoteComment",
		"note", "noteComment"}
	fildsMap map[string][]string = make(map[string][]string)
)

type message struct {
	Id      int
	Name    string
	Content string
	Time    int64
}

type messageComment struct {
	Id        int
	MessageId int
	Content   string
	Name      string
	Time      int64
}

type movieNote struct {
	Id    int
	Name  string
	Views int
	Time  int64
	Url   string
	Brief string
}

type movieNoteComment struct {
	Id          int
	MovieNoteId int
	Name        string
	Email       string
	Time        int64
	Content     string
	Website     string
	Pname       string
	Pcontent    string
}

type note struct {
	Id    int
	Name  string
	Time  int64
	Views int
}

type noteComment struct {
	Id       int
	NoteId   int
	Name     string
	Email    string
	Time     int64
	Content  string
	Website  string
	Pname    string
	Pcontent string
}

func init() {
	var v1 message
	registeAllFildName("message", v1)
	var v2 messageComment
	registeAllFildName("messageComment", v2)
	var v3 movieNote
	registeAllFildName("movieNote", v3)
	var v4 movieNoteComment
	registeAllFildName("movieNoteComment", v4)
	var v5 note
	registeAllFildName("note", v5)
	var v6 noteComment
	registeAllFildName("noteComment", v6)
}

func registeAllFildName(name string, obj interface{}) {
	t := reflect.TypeOf(obj)
	if t.Kind() == reflect.Ptr {
		t = t.Elem()
	}
	result := make([]string, 0)
	for i := 0; i < t.NumField(); i++ {
		fildName := t.Field(i).Name
		result = append(result, fildName)
	}
	fildsMap[name] = result
	return
}

func getAllTableName(c *gin.Context) {
	c.JSON(200, gin.H{
		"list": tableNames,
	})
}

func getAllFildsList(c *gin.Context) {
	name := c.Query("name")
	if _, ok := fildsMap[name]; ok {
		c.JSON(200, gin.H{
			"list": fildsMap[name],
		})
	} else {
		c.Error(errors.New("不存在该表: " + name))
	}
}

func getAllRows(c *gin.Context) {
	name := c.Query("name")
	switch name {
	case "message":
		getAllMessage(c)
	case "messageComment":
		getAllMessageComment(c)
	case "movieNote":
		getMoiveNoteList(c)
	case "movieNoteComment":
		getMovieNoteCommentLis(c)
	case "note":
		getAllNotes(c)
	case "noteComment":
		getAllNoteComments(c)
	default:
		c.JSON(500, gin.H{
			"err": "不存在该表",
		})
	}
}

func delRowById(c *gin.Context) {
	name := c.Query("name")
	switch name {
	case "message":
		delMessageById(c)
	case "messageComment":
		delMessageById(c)
	case "movieNote":
		delMovieNoteById(c)
	case "movieNoteComment":
		delMovieNoteCommentById(c)
	case "note":
		delNoteById(c)
	case "noteComment":
		delNoteCommentById(c)
	default:
		c.JSON(500, gin.H{
			"err": "不存在该表",
		})
	}
}

func InitDB(r *gin.Engine) {
	g := r.Group("/db")
	g.POST("/getAllTableName", getAllTableName)
	g.POST("/getAllFildsList", getAllFildsList)
	g.POST("/getAllRows", getAllRows)
	g.POST("/delRowById", delRowById)
}
